import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;

public class WechatLogin {

    public static String getUserInfo(String accessToken, String openId) {
        String apiUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId;
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(apiUrl);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            } else {
                System.out.println("请求失败，状态码: " + statusCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        // 替换为实际的 accessToken 和 openId
        String accessToken = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJvT3JMLTYxSjBKQVhLN0d4X245WXo2UnFYaEFFIiwiaWF0IjoxNzQ0MDc0Mzc3LCJleHAiOjE3NDQxNjA3Nzd9.vnSjcfRlBu4U6eE2Mcd7tfwd0yRDbfkYzAtMH1YmdbE";
        String openId = "oOrL-61J0JAXK7Gx_n9Yz6RqXhAE";
        String userInfo = getUserInfo(accessToken, openId);
        if (userInfo != null) {
            System.out.println("用户信息: " + userInfo);
        }
    }
}
